--[[
	Abilities
]]

--[[
	Specific Spell Attacks
]]
function ccR_Hunter_Focus_Shot()
	if (ccrSpec() == 1 or ccrSpec() == 3)
	and ccrPlayerLevel() >= 81
	and ccrPlayerHasSpell(COBRA_SHOT)
	then
		ccR_Hunter_Cobra_Shot()
	else
		ccR_Hunter_Steady_Shot()
	end
end
function ccR_Hunter_Steady_Shot()
	if ccrPlayerHasSpell(STEADY_SHOT)
	and ccrSpellCanAttack(STEADY_SHOT)
	then
		ccrCastAttack(STEADY_SHOT)
		return
	end
	return
end
function ccR_Hunter_Cobra_Shot()
	if ccrPlayerHasSpell(COBRA_SHOT)
	and ccrSpellCanAttack(COBRA_SHOT)
	then
		ccrCastAttack(STEADY_SHOT)
		return
	end
	return
end
function ccR_Hunter_Arcane_Shot()
	if ccrPlayerHasSpell(ARCANE_SHOT)
	and ccrSpellCanAttack(ARCANE_SHOT)
	then
		ccrCastAttack(ARCANE_SHOT)
		return
	end
	return
end
function ccR_Hunter_Concussive_Shot()
	local vUseAbility = ccRS["Hunter_UseConcussiveShot"]
	if vUseAbility
	and ccrPlayerHasSpell(CONCUSSIVE_SHOT)
	and ccrSpellCanAttack(CONCUSSIVE_SHOT)
	and not ccrTargetIsBoss()
	and ccrTargetIsMoving()
	then
		ccrCastAttack(CONCUSSIVE_SHOT)
		return
	end
	return
end

vLast_Serpent_Sting_Check = 0.1
function ccR_Hunter_Serpent_Sting()
	if ccrPlayerHasSpell(SERPENT_STING)
	and ccrSpellCanAttack(SERPENT_STING)
	and not ccrTargetHasMyDebuff(SERPENT_STING)
	and (GetTime()-vLast_Serpent_Sting_Check) > 1.5
	then
		ccrCastAttack(SERPENT_STING)
		vLast_Serpent_Sting_Check = GetTime()
		return
	end
	return
end

function ccR_Hunter_Mend_Pet()
	local vUseAbility = ccRS["Hunter_UseMendPet"]
	if vUseAbility
	and ccrPlayerHasSpell(MEND_PET)
	and ccrUnitHealth("Pet") > 0
	and ccrUnitHealth("Pet") < 75
	and ccrDistance("Player", "Pet") < 48
	then
		if ccrSpellIsReady(MEND_PET)
		and not ccrUnitHasBuff("Pet", MEND_PET)
		then
			ccrCast(MEND_PET)
		end
	end
end

function ccR_Scatter_Shot()
	if ccrPlayerHasSpell(SCATTER_SHOT)
	and ccrTargetIsCastingOrChanneling()
	and ccrSpellCanAttack(SCATTER_SHOT)
	then
		ccrCastAttack(SCATTER_SHOT)
		return
	end
	return
end

vLast_Scatter_Shot_Check = 0.1
function ccR_Scatter_Shot_On_Anyone()
	local vUseAbility = ccRS["Hunter_UseCounterShot"]
	if vUseAbility
	and ccrPlayerHasSpell(SCATTER_SHOT)
	and ccrSpellIsReady(SCATTER_SHOT)
	and (GetTime()-vLast_Scatter_Shot_Check) > 1.0
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and obj:InLineOfSight()
				and ccrUnitIsCastingOrChanneling(obj)
				and Player:IsFacing(obj)
				and ccrSpellCanAttack(SCATTER_SHOT, obj)
				then
					if ccrUnitIsCasting(obj)
					and ccrUnitHasLeftOnCast(obj, 1)
					then
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						obj:CastSpellByName(GetSpellInfo(SCATTER_SHOT))
						return
					end
					if ccrUnitIsChanneling(obj) then
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end						
						obj:CastSpellByName(GetSpellInfo(SCATTER_SHOT))
						return
					end									
				end
			end
		end	
		vLast_Scatter_Shot_Check = GetTime()
	end
end

function ccR_Counter_Shot()
	if ccrPlayerHasSpell(COUNTER_SHOT)
	and ccrTargetIsCastingOrChanneling()
	and ccrSpellCanAttack(COUNTER_SHOT)
	then
		ccrCastAttack(COUNTER_SHOT)
		return
	end
	return
end

vLast_Counter_Shot_Check = 0.1
function ccR_Counter_Shot_On_Anyone()
	local vUseAbility = ccRS["Hunter_UseCounterShot"]
	if vUseAbility
	and ccrPlayerHasSpell(COUNTER_SHOT)
	and ccrSpellIsReady(COUNTER_SHOT)
	and (GetTime()-vLast_Counter_Shot_Check) > 1.0
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and obj:InLineOfSight()
				and ccrUnitIsCastingOrChanneling(obj)
				and Player:IsFacing(obj)
				and ccrSpellCanAttack(COUNTER_SHOT, obj)
				then
					if ccrUnitIsCasting(obj)
					and ccrUnitHasLeftOnCast(obj, 1)
					then
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						obj:CastSpellByName(GetSpellInfo(COUNTER_SHOT))
						return
					end
					if ccrUnitIsChanneling(obj) then
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
						obj:CastSpellByName(GetSpellInfo(COUNTER_SHOT))
						return
					end									
				end
			end
		end	
		vLast_Counter_Shot_Check = GetTime()
	end
end

function ccR_Hunter_Masters_Call()
	local vUseAbility = ccRS["Hunter_UseMastersCall"]
	if vUseAbility
	and ccrPlayerHasSpell(MASTERS_CALL)
	and ccrSpellIsReady(MASTERS_CALL)
	and ccrPetIsAlive()
	and Pet and Pet:Exists() and Player:GetDistance(Pet) <= 38
	then
		local eventIndex = C_LossOfControl.GetNumEvents()
		while (eventIndex > 0) do
			local _, _, text = C_LossOfControl.GetEventInfo(eventIndex)
			if text == LOSS_OF_CONTROL_DISPLAY_ROOT 
			or text == LOSS_OF_CONTROL_DISPLAY_SNARE
			or text == LOSS_OF_CONTROL_DISPLAY_STUN_MECHANIC
			or text == LOSS_OF_CONTROL_DISPLAY_STUN
			then
				ccrCast(MASTERS_CALL)
				return
			end
			eventIndex = eventIndex - 1
		end
	end
end

function ccR_Hunter_Tank_Misdirection()
	if ccRS.Hunter_UseMisdirectionOnTank then 
		local vGroupType
		local vTank
		local objTank
		if IsInGroup() then vGroupType = "party" end;
		--if IsInRaid() then vGroupType = "raid" end;
		if vGroupType == nil then return end;
		for i = 0, GetNumGroupMembers() do
			if ccrUnitIsTank(vGroupType .. tostring(i)) then vTank = vGroupType .. tostring(i) end;
		end
		if vTank ~= nil or GetNumGroupMembers() == 3 then
			objTank = GetObjectFromUnitID(vTank)
			if objTank and objTank:Exists() and not objTank:IsDead() and objTank:InLineOfSight() then
				if not onCD(MISDIRECTION)
				and not ccrPlayerHasBuff(MISDIRECTION)
				and not ccrUnitHasBuff(vTank, TRICKS_OF_THE_TRADE)
				and not ccrUnitHasBuff(vTank, MISDIRECTION)
				and ccrTargetName() ~= "Weak Spot"
				then
					ccrCast(MISDIRECTION, vTank)
				end
			end
		end
	end
end

function ccR_Hunter_Pet_Misdirection()
	if ccRS.Hunter_Pet_Misdirection then
		local isTanking, status, scaledPercent, rawPercent, threatValue = UnitDetailedThreatSituation("player", "target")
		local objPet = GetObjectFromUnitID("Pet")
		if ccrPetIsAlive()
		and not SpellIsOnCooldown("Misdirection")
		and not ccrPlayerHasBuff("Misdirection")
		and scaledPercent[3]~=nil
		and scaledPercent[3]>=75
		and objPet and objPet:Exists() and not objPet:IsDead() and objPet:InLineOfSight()
		then
			ccrCast(MISDIRECTION, "Pet")
		end
	end
end

--[[
	Survival
]]
function ccR_Hunter_Explosive_Shot()
	if ccrPlayerHasSpell(EXPLOSIVE_SHOT)
	and ccrSpellIsReady(EXPLOSIVE_SHOT)
	and ccrSpellCanAttack(EXPLOSIVE_SHOT)
	then
		ccrCastAttack(EXPLOSIVE_SHOT)
		return
	end
end
function ccR_Hunter_Black_Arrow()
	if ccrPlayerHasSpell(BLACK_ARROW)
	and ccrSpellIsReady(BLACK_ARROW)
	and ccrSpellCanAttack(BLACK_ARROW)
	then
		ccrCastAttack(BLACK_ARROW)
		return
	end
end
